<script setup lang="ts">
import { toRefs, watch } from 'vue';

import { IconifyIcon } from '@vben/icons';

import {
  Alert,
  Button,
  Col,
  FormItem,
  Input,
  Row,
  Select,
  SelectOption,
} from 'ant-design-vue';

import { $t } from '#/locales';

import { useFormFields } from '../../../helpers';
import HttpRequestParamSetting from './http-request-param-setting.vue';

defineOptions({ name: 'HttpRequestSetting' });

const props = defineProps({
  setting: {
    type: Object,
    required: true,
  },
  responseEnable: {
    type: Boolean,
    required: true,
  },
  formItemPrefix: {
    type: String,
    required: true,
  },
});

const emits = defineEmits(['update:setting']);

const { setting } = toRefs(props);

watch(
  () => setting,
  (val) => {
    emits('update:setting', val);
  },
);

/** 流程表单字段 */
const formFields = useFormFields();

/** 添加 HTTP 请求返回值设置项 */
function addHttpResponseSetting(responseSetting: Record<string, string>[]) {
  responseSetting.push({
    key: '',
    value: '',
  });
}

/** 删除 HTTP 请求返回值设置项 */
function deleteHttpResponseSetting(
  responseSetting: Record<string, string>[],
  index: number,
) {
  responseSetting.splice(index, 1);
}
</script>
<template>
  <FormItem>
    <Alert
      :message="$t('components.simpleProcessDesigner.nodes.httpRequest.alertMessage')"
      type="warning"
      show-icon
      :closable="false"
    />
  </FormItem>
  <!-- 请求地址-->
  <FormItem
    :label="$t('components.simpleProcessDesigner.nodes.httpRequest.formSchema.url')"
    :label-col="{ span: 24 }"
    :wrapper-col="{ span: 24 }"
    :name="[formItemPrefix, 'url']"
    :rules="{
      required: true,
      message: $t('components.simpleProcessDesigner.nodes.httpRequest.formRule.urlRequired'),
      trigger: ['blur', 'change'],
    }"
  >
    <Input v-model:value="setting.url" :placeholder="$t('components.simpleProcessDesigner.nodes.httpRequest.formSchema.urlPlaceholder')" />
  </FormItem>
  <!-- 请求头，请求体设置-->
  <HttpRequestParamSetting
    :header="setting.header"
    :body="setting.body"
    :bind="formItemPrefix"
  />
  <!-- 返回值设置-->
  <div v-if="responseEnable">
    <FormItem
      :label="$t('components.simpleProcessDesigner.nodes.httpRequest.formSchema.response')"
      :label-col="{ span: 24 }"
      :wrapper-col="{ span: 24 }"
    >
      <Alert
        :message="$t('components.simpleProcessDesigner.nodes.httpRequest.responseAlertMessage')"
        type="warning"
        show-icon
        :closable="false"
      />
    </FormItem>
    <FormItem :wrapper-col="{ span: 24 }">
      <Row
        :gutter="8"
        v-for="(item, index) in setting.response"
        :key="index"
        class="mb-2"
      >
        <Col :span="10">
          <FormItem
            :name="[formItemPrefix, 'response', index, 'key']"
            :rules="{
              required: true,
              message: $t('components.simpleProcessDesigner.nodes.httpRequest.formRule.responseKeyRequired'),
              trigger: ['blur', 'change'],
            }"
          >
            <Select
              v-model:value="item.key"
              :placeholder="$t('components.simpleProcessDesigner.nodes.httpRequest.formSchema.responseKeyPlaceholder')"
              allow-clear
            >
              <SelectOption
                v-for="(field, fIdx) in formFields"
                :key="fIdx"
                :label="field.title"
                :value="field.field"
                :disabled="!field.required"
              >
                {{ field.title }}
              </SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem
            :name="[formItemPrefix, 'response', index, 'value']"
            :rules="{
              required: true,
              message: $t('components.simpleProcessDesigner.nodes.httpRequest.formRule.responseValueRequired'),
              trigger: ['blur', 'change'],
            }"
          >
            <Input v-model:value="item.value" :placeholder="$t('components.simpleProcessDesigner.nodes.httpRequest.formSchema.responseValuePlaceholder')" />
          </FormItem>
        </Col>
        <Col :span="2">
          <div class="flex h-8 items-center">
            <IconifyIcon
              class="size-4 cursor-pointer text-red-500"
              icon="lucide:trash-2"
              @click="deleteHttpResponseSetting(setting.response!, index)"
            />
          </div>
        </Col>
      </Row>
      <Button
        type="link"
        @click="addHttpResponseSetting(setting.response!)"
        class="flex items-center"
      >
        <template #icon>
          <IconifyIcon class="size-4" icon="lucide:plus" />
        </template>
        {{ $t('components.simpleProcessDesigner.nodes.httpRequest.addRow') }}
      </Button>
    </FormItem>
  </div>
</template>
